<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class PhotoController extends Controller
{
	public function getPhoto(Request $request)
	{
		// 获取分页
		// echo 222;
		$num = $request->input('num',5);

		// dd($res);

		if($request->input('keywords'))
		{
			$res=DB::table('photo')->join('pcate','pcate.id','=','photo.pid')->select('photo.*','pcate.uid')->where('pcate.uid','like','%'.$request->input('keywords').'%')->paginate($num);
		}else{
			$res=DB::table('photo')->join('pcate','pcate.id','=','photo.pid')->select('photo.*','pcate.uid')->paginate($num);
		}

		// $res = DB::table('photo')->get();
		return view('admin.photo.photo',['res'=>$res]);
	}

	// 图片修改
	public function getEdit($id)
	{
		$photo =DB::table('photo')->join('pcate','pcate.id','=','photo.pid')->select('photo.*','pcate.uid','pcate.pname')->where('photo.id',$id)->first();
		$cates=DB::table('pcate')->get();
		// dd($photo,$cates);
		// 解析
		return view('admin.photo.edit',['photo'=>$photo,'cates'=>$cates]);
	}

	// 修改
	public function postUpdate(Request $request)
	{
		// 获取id

		$id = $request->only(['id']);

		$data = $request->only(['pid','pname']);

		// 执行
		$res = DB::table('photo')->where('id',$id)->update($data);

// dd($res);
		// 判断
		if($res)
		{
			return redirect('/admin/photo/photo')->with('success','修改成功');
		}else{
			return back()->with('error','修改失败');
		}
	}

	public function postDelete(Request $request)
	{
		// 获取id
		$id = $request->input('id');
		// dd($id);
		// 执行删除
		$res = DB::table('photo')->where('id',$id)->delete();
		if($res){
			echo 1;
		}else{
			echo 0;
		}
	}

}

?>